#include <iostream>
#include <string>
using namespace std;

struct Node {
	string val;
	Node* left = nullptr;
	Node* right = nullptr;
};
string sp;
Node* H = nullptr;
void getTree(Node*& h)
{
	string cur;
	cin >> cur;
	if (cur == sp) return;
	h = new Node;
	h->val = cur;
	getTree(h->left);
	getTree(h->right);
}

void prev(Node* h)
{
	if (!h) return;
	if (h != H) cout << ',';
	cout << h->val;
	prev(h->left);
	prev(h->right);
}

bool j = false;
void in(Node* h)
{
	if (!h) return;
	in(h->left);
	if (j) cout << ',';
	cout << h->val; j = true;
	in(h->right);
}
void post(Node* h)
{
	if (!h) return;
	post(h->left);
	post(h->right);
	cout << h->val;
	if (h != H) cout << ',';
}
int main()
{
	cin >> sp;
	getTree(H);

	prev(H);
	cout << endl;
	in(H);
	cout << endl;
	post(H);

	return 0;
}